package com.otaku.member.service;

import com.otaku.member.interfaceApi.SendMailInterface;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class Consumer {

    @StreamListener(SendMailInterface.SEND_MAIL_ORDER)
    public void mailListener(String keys) {
        System.out.println("send_mail_order" + keys);
    }

    @StreamListener(SendMailInterface.SEND_MSG_ORDER)
    public void msgListener(String keys) {
        System.out.println("send_msg_order" + keys);
    }

    public static void main(String[] args) {
        /**
         * 死锁
         * 定义两个list a,b 两个线程1,2 线程1锁ab 线程2 锁ba
         */
        List<Integer> a = Arrays.asList(1, 2, 3);
        List<Integer> b = Arrays.asList(4, 5, 6);
        new Thread(() -> {
            synchronized (a) {
                for (int i = 0; i < a.size() - 1; i++) {
                    System.out.println(a.get(i));
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b) {
                    for (int i = 0; i < b.size() - 1; i++) {
                        System.out.println(b.get(i));
                    }
                }
            }
        }).start();
        new Thread(() -> {
            synchronized (b) {
                for (int i = 0; i < b.size() - 1; i++) {
                    System.out.println(b.get(i));
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a) {
                    for (int i = 0; i < a.size() - 1; i++) {
                        System.out.println(a.get(i));
                    }
                }
            }
        }).start();
    }
}
